package cool.taomu.toolkit.jersey.jetty

import com.google.auto.service.AutoService
import com.google.inject.AbstractModule
import com.google.inject.TypeLiteral
import com.google.inject.name.Names
import cool.taomu.toolkit.TaomuConfigure
import cool.taomu.toolkit.TaomuGuice
import cool.taomu.toolkit.jersey.jetty.provider.JettyProvider
import cool.taomu.toolkit.platform.inter.AbsPlatform
import cool.taomu.toolkit.platform.inter.IPlatform
import java.util.ArrayList
import java.util.List
import org.eclipse.jetty.server.Server

@AutoService(IPlatform)
class JerseryJettyServerPlugin extends AbsPlatform {

    override note() {
        "Jserey框架Jetty服务器插件"
    }

    override packages() {
    }

    override module(TaomuConfigure config) {
        new AbstractModule() {
            override configure() {
                val list = new ArrayList<String>();
                config.binder(TaomuGuice.JESERY_PACKAGES, [
                    if (it instanceof String) {
                        list.add(it)
                    }
                ])
                bind(new TypeLiteral<List<String>>() {
                }).annotatedWith(Names.named(TaomuGuice.JESERY_PACKAGES)).toInstance(list)
                bind(Server).toProvider(JettyProvider);
                bind(JerseryJettyServer).asEagerSingleton;
            }
        }
    }

    override start() {
        var inject = this.context.injectorSupplier.get
        val server = inject.getInstance(JerseryJettyServer)
        this.context.executor.execute([
            server.start();
        ])
    }

    override init() {
    }

}
